//
//  MyCollectionViewController.m
//  Exam13-2
//
//  Created by 孙晓晔 on 2017/4/26.
//  Copyright © 2017年 孙晓晔. All rights reserved.
//

#import "MyCollectionViewController.h"
#import "MyCollectionViewCell.h"
#import "CalculatorView.h"

#import "TallyType.h"


@interface MyCollectionViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,CalculatorViewDelegate>

@property(nonatomic,strong) NSMutableArray *typeArr;

@property (nonatomic,strong)CalculatorView *calview;

@end

@implementation MyCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes
//    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    
//    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    self.title = @"新增";
    
    int width = ([UIScreen mainScreen].bounds.size.width - 25) / 4;
    
    NSLog(@"width = %d", width);
    
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.calview];
}

//返回位置
- (void)backPositionWithAnimation {
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.calview.frame = CGRectMake(0, self.view.frame.size.height * 0.6, self.view.frame.size.width, self.view.frame.size.height * 0.4);
    } completion:nil];
}

//计算器界面
- (CalculatorView *)calview {
    if (!_calview) {
        _calview = [[CalculatorView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height * 0.6, self.view.frame.size.width, self.view.frame.size.height * 0.4)];
        _calview.delegate = self;
        // _calview.imageViewSize = self.itemSize;
        
        
    }
    return _calview;
}

-(NSMutableArray *)typeArr {
    if (_typeArr == nil) {
        NSString *strPath = [[NSBundle mainBundle]pathForResource:@"type.plist" ofType:nil];
        NSArray *arr = [NSArray arrayWithContentsOfFile:strPath];
        
        NSMutableArray *muArr = [NSMutableArray array];
        
        for (NSDictionary *dict in arr) {
            TallyType *obj = [TallyType tallyTypeWithDict:dict];
            
            [muArr addObject:obj];
        }
        
        _typeArr = muArr;
    }
    
    return _typeArr;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.typeArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = (MyCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    
    // Configure the cell
    TallyType *type = self.typeArr[indexPath.row];
    
    cell.imageView.image = [UIImage imageNamed:type.tallyCellImage];
    cell.titleLabel.text = type.tallyCellName;
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

//定义每一个cell的大小

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//    
//    return CGSizeMake(115, 100);
//}

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"didSelectItemAtIndexPath",nil);
    
    TallyType *type = self.typeArr[indexPath.row];
    
//    CGRect itemRect = CGRectMake(0, 0, 60, 60);
    
    MyCollectionViewCell *cell = (MyCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    
    CGRect cellRect = [collectionView convertRect:cell.frame fromView:collectionView];
    
    CGRect imgInCellRect = cell.imageView.frame;
    CGFloat x = cellRect.origin.x + imgInCellRect.origin.x;
    CGFloat y = cellRect.origin.y + imgInCellRect.origin.y + 64;
    //图片在collectionView的位置
    CGRect itemRect = CGRectMake(x, y, imgInCellRect.size.width, imgInCellRect.size.height);
    
    UIImageView *animateImage = [[UIImageView alloc] initWithFrame:itemRect];
    animateImage.image = [UIImage imageNamed:type.tallyCellImage];
    [self.view addSubview:animateImage];
    
    __block CGPoint imgPoint;
    self.calview.positionInViewBlock = ^(CGPoint point){
        imgPoint = point;
    };
    
    //给计算器界面传值
    self.calview.imageViewSize = itemRect.size;
    self.calview.image = [UIImage imageNamed:type.tallyCellImage];;
    self.calview.typeName = type.tallyCellName;
    
    //设置贝塞尔曲线路径动画
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:animateImage.center];
    [path addCurveToPoint:imgPoint controlPoint1:CGPointMake(animateImage.frame.origin.x, animateImage.frame.origin.y-100 ) controlPoint2:CGPointMake(animateImage.frame.origin.x, animateImage.frame.origin.y-100 )];
    CAKeyframeAnimation *anmiation0 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    anmiation0.path = path.CGPath;
    anmiation0.duration = 1;
    anmiation0.removedOnCompletion = NO;
    anmiation0.fillMode = kCAFillModeForwards;
    [animateImage.layer addAnimation:anmiation0 forKey:nil];
    [NSTimer scheduledTimerWithTimeInterval:1 repeats:NO block:^(NSTimer * _Nonnull timer) {
        [animateImage removeFromSuperview];
    }];
}


// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}



// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    NSLog(@"scrollViewDidEndDragging",nil);
    
    CGFloat bottomY = self.collectionView.contentSize.height - self.collectionView.frame.size.height;
    if (scrollView.contentOffset.y >= bottomY) {
        NSLog(@"计算器下去");
        [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
            self.calview.frame = CGRectMake(0, CGRectGetHeight(self.view.frame)-CGRectGetHeight(self.calview.frame)/5, self.calview.frame.size.width, self.calview.frame.size.height);
        } completion:nil];
    }
    
}

//计算界面下降
- (void)listScrollToBottom {
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.calview.frame = CGRectMake(0, CGRectGetHeight(self.view.frame)-CGRectGetHeight(self.calview.frame)/5, self.calview.frame.size.width, self.calview.frame.size.height);
    } completion:nil];
    
    
}

@end
